import axios from 'axios'
import qs from 'qs'
import store from '../vuex/store'
import md5 from 'js-md5';
import Vue from 'vue';
import {confirmFun} from "../utils/index.js"
console.log(confirmFun,"confirmFun");
// confirmFun("token失效，请重新登录啊","warning",()=>{location.replace("/#/")})
// location.replace("/home","xx")

// axios 配置
// axios.defaults.timeout = 5000; //延迟模拟
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
//axios.defaults.baseURL = 'http://newtest.kqmis.com/personal';

//POST传参序列化，以及全局token
axios.interceptors.request.use((config) => {

    //  config.headers['Access-Control-Allow-Origin'] = '*';
    //  config.headers['Access-Control-Allow-Methods']='POST';
    //  config.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type';

    // if (store.state.login.userInfo[0].token) {
    //    // 判断是否存在用户编码，如果存在的话，则每个http header都加上用户编码
    //    config.headers.renshiSecret =`${md5(store.state.login.userInfo[0].token)}`;
    // }

    if(config.method  === 'post'){
        config.data = qs.stringify(config.data);
    }

    return config;
},(error) =>{   // 这里是返回状态码不为200时候的错误处理
    console.log(error,"error")
    return Promise.reject(error);
});

// 添加请求拦截器 (可以在这里加token， config.headers.Authorization = `token ${store.state.token}`)
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么(status == 200)
    return config;
  }, function (error) {
    // 对请求错误做些什么

    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    if (response.data.code == 300 && response.data.message == "token失效，请重新登录") {
        confirmFun("登录超时，请重新登录","warning",()=>{location.replace("/#/")});
        return response;
    } else {
        return response;
    }   
  }, function (error) {
    // 对响应错误做点什么(status != 200)
    return Promise.reject(error);
});

export function post(url, params) {
    if( store.state.login.userInfo[0].YHBM && params){
        params.YHBM = store.state.login.userInfo[0].YHBM;
        params.token = store.state.login.userInfo[0].token;
    } else if (!params) {
        let par = {
            YHBM: store.state.login.userInfo[0].YHBM,
            token: store.state.login.userInfo[0].token,
        }
        params = par;
    }
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
               reject(error)
            })
    })
}

export function get(url, params) {
    let symbol = '?';
    if(url.indexOf('?') != -1){
        symbol = '&'
    }
    if(store.state.login.userInfo[0].YHBM){
        url = `${url}${symbol}YHBM=${store.state.login.userInfo[0].YHBM}`;
    }

    if(store.state.login.userInfo[0].token){
        url = `${url}&token=${store.state.login.userInfo[0].token}`;
    }

    return new Promise((resolve, reject) => {
        axios.get(url, params)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
               reject(error)
            })
    })
}

export default {
    get,
    post
}
